package libs.yzx.zaza.acts;

import android.os.Bundle;
import android.os.SystemClock;
import android.renderscript.Int2;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/8/16
 */
public class RXJavaActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new Button(this));

        String array[] = {"1", "2"};
        Observable
                .create(new Observable.OnSubscribe<String>() {
                    public void call(Subscriber<? super String> subscriber) {
                        SystemClock.sleep(2000);
                        subscriber.onCompleted();
                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    public void onCompleted() {
                        Toast.makeText(RXJavaActivity.this, "complete", Toast.LENGTH_SHORT).show();
                    }

                    public void onError(Throwable e) {
                    }

                    public void onNext(String s) {
                    }
                });


        Observable.from(new Int2[]{})
                .filter(new Func1<Int2, Boolean>() {
                    @Override
                    public Boolean call(Int2 int2) {
                        return true;
                    }
                })
                .flatMap(new Func1<Int2, Observable<Integer>>() {
                    @Override
                    public Observable<Integer> call(Int2 i) {
                        return Observable.from(new Integer[]{i.x, i.y});
                    }
                })
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(Integer integer) {
                    }
                });


    }

}
